package com.siwei.mes.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.system.CalibrationRecord;
import com.siwei.mes.entity.system.Equipment;
import com.siwei.mes.mapper.system.CalibrationRecordMapper;
import com.siwei.mes.mapper.system.EquipmentMapper;
import com.siwei.mes.service.system.CalibrationRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 设备校准记录表(CalibrationRecord)表服务实现类
 *
 * @author dmwang
 * @since 2024-07-18 18:59:19
 */
@Service
public class CalibrationRecordServiceImpl implements CalibrationRecordService {
    @Resource
    private CalibrationRecordMapper calibrationRecordMapper;
    @Resource
    private EquipmentMapper equipmentMapper;

    @Override
    public Long create(CalibrationRecord entity) {
        entity.init(true);
        calibrationRecordMapper.insertSelective(entity);

        /**修改对应仪器的上次检测时间*/
        Equipment equipment = new Equipment();
        equipment.setId(entity.getEquipmentId());
        equipment.setLastDetectionTime(new Date());
        equipment.init(false);
        equipmentMapper.updateByPrimaryKeySelective(equipment);
        return entity.getId();
    }

    @Override
    public void update(CalibrationRecord entity) {
        CalibrationRecord calibrationRecord = calibrationRecordMapper.selectByPrimaryKey(entity.getId());
        assertTrue(calibrationRecord == null, "设备校准记录表信息不存在");
        entity.init(false);
        calibrationRecordMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        CalibrationRecord calibrationRecord = calibrationRecordMapper.selectByPrimaryKey(id);
        assertTrue(calibrationRecord == null, "设备校准记录表信息不存在");
        calibrationRecordMapper.deleteByPrimaryKey(calibrationRecord);
    }

    @Override
    public PageInfo<CalibrationRecord> getPage(PageQuery<CalibrationRecord, CalibrationRecord> query) {
        CalibrationRecord searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> calibrationRecordMapper.getList(searchParam));
    }

    @Override
    public CalibrationRecord loadById(Long id) {
        return calibrationRecordMapper.loadById(id);
    }

    @Override
    public List<CalibrationRecord> getAll(CalibrationRecord entity) {
        return calibrationRecordMapper.getList(entity);
    }
}
